﻿Imports System.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.DNN.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports DotNetNuke.Services.Localization
Imports Aricie.Collections
Imports Aricie.ComponentModel
Imports Aricie.DNN.UI.WebControls.EditControls
Imports System.Linq

Namespace Aricie.DNN.Services.Url.FriendlyUrl

    Public Delegate Function UrlRewriter(ByVal group As UrlGroup, ByVal params As UrlRewriteInput, ByVal context As FriendlierUrlContext) As GroupRewriteResult
    Public Delegate Function UrlReverser(ByVal radixMatch As String, ByVal group As UrlGroup, ByVal context As FriendlierUrlContext) As GroupRewriteResult



    <Serializable()> _
    <DefaultProperty("DisplayName")> _
    Public Class GroupParamsRewrite

        Private _GroupName As String

        Private _Description As String
        Private _RewriteType As RewriteType
        Private _UrlParams As New SerializableDictionary(Of UrlParam, ParamNature)
        Private _LookupRegexPattern As String
        Private _SupportsEntityToken As Boolean
        Private _UrlRewriter As UrlRewriter
        Private _UrlReverser As UrlReverser
        Private _Localized As Boolean




        Public Sub New()

        End Sub

        Public Sub New(ByVal groupName As String, ByVal description As String, ByVal rewriteType As RewriteType)
            Me._GroupName = groupName
            Me._Description = description
            Me._RewriteType = rewriteType
        End Sub

        Public Sub New(ByVal groupName As String, ByVal description As String, ByVal rewriteType As RewriteType, _
                        ByVal ParamArray urlParams() As UrlParam)
            Me.New(groupName, description, rewriteType)
            For Each paramName As UrlParam In urlParams
                Me._UrlParams(paramName) = ParamNature.Mandatory
            Next
        End Sub
        Public Sub New(ByVal groupName As String, ByVal description As String, ByVal rewriteType As RewriteType, ByVal lookupRegexPattern As String)
            Me.New(groupName, description, rewriteType)
            Me._LookupRegexPattern = lookupRegexPattern
        End Sub

        Public Sub New(ByVal groupName As String, ByVal description As String, ByVal rewriteType As RewriteType, ByVal lookupRegexPattern As String, _
                        ByVal ParamArray urlParams() As UrlParam)
            Me.New(groupName, description, rewriteType, urlParams)
            Me._LookupRegexPattern = lookupRegexPattern
        End Sub

        <ExtendedCategory("GeneralSettings")> _
        <MainCategory()> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property GroupName() As String
            Get
                Return _GroupName
            End Get
            Set(ByVal value As String)
                _GroupName = value
            End Set
        End Property

        <ExtendedCategory("GeneralSettings")> _
        <Browsable(False)> _
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal value As String)
                _Description = value
            End Set
        End Property

        <ExtendedCategory("GeneralSettings")> _
        <System.Xml.Serialization.XmlIgnore()> _
        Public ReadOnly Property LocalizedDescription() As String
            Get
                Dim toReturn As String = Localization.GetString("Group_" & Me.GroupName & ".Description", _
                               NukeHelper.GetModuleSharedResourceFile(FriendlierUrlContext.MODULE_NAME))
                'Return toReturn
                If (Not String.IsNullOrEmpty(toReturn)) AndAlso (Not toReturn.ToLowerInvariant.StartsWith("resx:")) Then
                    Return toReturn
                End If
                Return Me.Description
            End Get
        End Property

        <ExtendedCategory("GeneralSettings")> _
        Public Property Localized() As Boolean
            Get
                Return _Localized
            End Get
            Set(ByVal value As Boolean)
                _Localized = value
            End Set
        End Property

        <ExtendedCategory("AdvancedDetails")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property RewriteType() As RewriteType
            Get
                Return _RewriteType
            End Get
            Set(ByVal value As RewriteType)
                _RewriteType = value
            End Set
        End Property

        <ExtendedCategory("AdvancedDetails")> _
        <Editor(GetType(DictionaryEditControl), GetType(EditControl)), _
          KeyEditor("Aricie.DNN.UI.WebControls.EditControls.PropertyEditorEditControl, Aricie.DNN", GetType(UrlParamsKeyAttributes)), _
          LabelMode(LabelMode.Top), IsReadOnly(False)> _
        Public Property UrlParams() As SerializableDictionary(Of UrlParam, ParamNature)
            Get
                Return _UrlParams
            End Get
            Set(ByVal value As SerializableDictionary(Of UrlParam, ParamNature))
                _UrlParams = value
            End Set
        End Property



        Private Class UrlParamsKeyAttributes
            Implements IAttributesProvider


            Public Function GetAttributes() As System.Collections.Generic.IEnumerable(Of System.Attribute) Implements DNN.UI.Attributes.IAttributesProvider.GetAttributes
                Dim toReturn As New List(Of Attribute)
                toReturn.Add(New FieldStyleAttribute("100%", "50%", "50%"))
                Return toReturn
            End Function
        End Class

        <ExtendedCategory("AdvancedDetails")> _
        <Browsable(False)> _
        Public Property LookupRegexPattern() As String
            Get
                Return _LookupRegexPattern
            End Get
            Set(ByVal value As String)
                _LookupRegexPattern = value
            End Set
        End Property

        Public Property SupportsEntityToken() As Boolean
            Get
                Return _SupportsEntityToken
            End Get
            Set(ByVal value As Boolean)
                _SupportsEntityToken = value
            End Set
        End Property

        Public Property UrlRewriter() As UrlRewriter
            Get
                Return _UrlRewriter
            End Get
            Set(ByVal value As UrlRewriter)
                _UrlRewriter = value
            End Set
        End Property

        Public Property UrlReverser() As UrlReverser
            Get
                Return _UrlReverser
            End Get
            Set(ByVal value As UrlReverser)
                _UrlReverser = value
            End Set
        End Property



        Public Function HasCompatibleType(ByVal otherType As RewriteType) As Boolean

            Return (Me.RewriteType And otherType) <> FriendlyUrl.RewriteType.Wrong

        End Function

#Region "Display"

        <Browsable(False)> _
        Public ReadOnly Property DisplayName As String
            Get
                Return GroupName & " ◄ " & String.Join(" | ", UrlParams.Keys.Select(Of String)(Function(kvp) String.Join(", ", kvp.SyntaxOptions().ToArray())).ToArray())
            End Get
        End Property

#End Region

        Public Overrides Function GetHashCode() As Integer
            Return _GroupName.GetHashCode()
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean

            Dim g As GroupParamsRewrite = TryCast(obj, GroupParamsRewrite)
            If g Is Nothing Then
                Return False
            Else
                Return _GroupName.Equals(g.GroupName)
            End If

        End Function

    End Class

    '1101
End Namespace
